package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.dto.CommentAddDTO;
import com.xbongbong.paas.pojo.dto.CommentAddOnCommentDTO;
import com.xbongbong.paas.pojo.dto.CommentDeleteDTO;
import com.xbongbong.paas.pojo.dto.CommentDetailDTO;
import com.xbongbong.paas.pojo.dto.CommentEditDTO;
import com.xbongbong.paas.pojo.dto.CommentListDTO;
import com.xbongbong.paas.pojo.dto.ReplyListDTO;
import com.xbongbong.paas.pojo.vo.CommentAddOnCommentVO;
import com.xbongbong.paas.pojo.vo.CommentAddVO;
import com.xbongbong.paas.pojo.vo.CommentDeleteVO;
import com.xbongbong.paas.pojo.vo.CommentDetailVO;
import com.xbongbong.paas.pojo.vo.CommentListVO;
import com.xbongbong.paas.pojo.vo.ReplyListVO;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.service.PaasCommentService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author: wufeng
 * @date: 2018/8/29 9:56
 * @desrcption: 评论操作
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/mobile/comment")
public class MobilePaasCommentController {

    @Resource
    private PaasCommentService paasCommentService;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    /**
     * 评论列表查询
     * @param commentListDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid CommentListDTO commentListDTO, BindingResult br) {
        XbbResponse<CommentListVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CommentListVO commentListVO = paasCommentService.list(commentListDTO);
                response = new XbbResponse<>(commentListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 评论流程
     * @param commentAddDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String commentProcess(@RequestBody @Valid CommentAddDTO commentAddDTO, BindingResult br) {
        XbbResponse<CommentAddVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(commentAddDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(commentAddDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                CommentAddVO commentAddVO = paasCommentService.add(commentAddDTO);
                response = new XbbResponse<>(commentAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String commentDetail(@RequestBody @Valid CommentDetailDTO commentDetailDTO, BindingResult br) {
        XbbResponse<CommentDetailVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CommentDetailVO commentDetailVO = new CommentDetailVO();
                commentDetailVO.setCommentList(paasCommentService.getCommentDetail(commentDetailDTO));
                response = new XbbResponse<>(commentDetailVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * Description: 在评论下添加评论
     * @param commentAddOnCommentDTO
     * @param br
     * @return java.lang.String
     * @author 魏荣杰
     * @date 2019/5/12 11:05
     * @since v1.0
     */
    @RequestMapping(value = "/addOnComment", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String addOnComment(@RequestBody @Valid CommentAddOnCommentDTO commentAddOnCommentDTO, BindingResult br) {
        XbbResponse<CommentAddOnCommentVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CommentAddOnCommentVO commentAddVO = paasCommentService.addOnComment(commentAddOnCommentDTO);
                response = new XbbResponse<>(commentAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 评论删除流程
     * @param commentDeleteDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String deleteComment(@RequestBody @Valid CommentDeleteDTO commentDeleteDTO, BindingResult br) {
        XbbResponse<CommentDeleteVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CommentDeleteVO commentDeleteVO = paasCommentService.delete(commentDeleteDTO);
                response = new XbbResponse<>(commentDeleteVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 工单动态编辑流程
     * @param commentEditDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String deleteComment(@RequestBody @Valid CommentEditDTO commentEditDTO, BindingResult br) {
        XbbResponse<CommentAddVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CommentAddVO commentAddVO = paasCommentService.edit(commentEditDTO);
                response = new XbbResponse<>(commentAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 评论回复列表查询
     * @param replyListDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/replyList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String replyList(@RequestBody @Valid ReplyListDTO replyListDTO, BindingResult br) {
        XbbResponse<ReplyListVO> response;
        if (br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ReplyListVO replyListVO = paasCommentService.replyList(replyListDTO);
                response = new XbbResponse<>(replyListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
